package cn.stronglink.assetmanage.widget.treeview;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.List;

import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.entity.Node;

/**
 * Created by chencongcong on 2018/6/19.
 */

public class SimpleTreeAdapter<T> extends TreeListViewAdapter<T> {
    private int selectPosition = -1;
    @Override
    public int getSelectPosition() {
        return selectPosition;
    }
    @Override
    public void setSelectPosition(int selectPosition) {
        this.selectPosition = selectPosition;
    }

    public SimpleTreeAdapter(ListView mTree, Context context, List<T> datas,
                             int defaultExpandLevel) throws IllegalArgumentException,
            IllegalAccessException {
        super(mTree, context, datas, defaultExpandLevel);
    }

    @Override
    public View getConvertView(Node node, int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_simple_tree, parent, false);
            viewHolder = new ViewHolder();
//            viewHolder.icon = (ImageView) convertView
//                    .findViewById(R.id.id_treenode_icon);
            viewHolder.label = (TextView) convertView
                    .findViewById(R.id.tv_tree_name);
            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (node.getIcon() == -1) {
//            viewHolder.icon.setVisibility(View.INVISIBLE);
        } else {
//            viewHolder.icon.setVisibility(View.VISIBLE);
//            viewHolder.icon.setImageResource(node.getIcon());
        }
        if (selectPosition == position) {
            viewHolder.label.setTextColor(Color.BLUE);
        } else {
            viewHolder.label.setTextColor(Color.parseColor("#323537"));
        }
        viewHolder.label.setText(node.getName());

        return convertView;
    }

    private final class ViewHolder {
        ImageView icon;
        TextView label;
    }
}
